package com.ayoka.ui.assignment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.databinding.ActivityCourseAssignmentDescriptionBinding;
import com.ayoka.mvp.CourseAssignmentDescriptionMVP.CourseAssignmentDescriptionPresenter;
import com.ayoka.mvp.CourseAssignmentDescriptionMVP.ICourseAssignmentDescriptionView;
import com.ayoka.utils.MediaManager;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jincp.image.load.ImageLoaderProxy;
import com.jzvd.JZMediaIjkplayer;
import com.jzvd.SimpleJZMediaPlayerListener;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class CourseAssignmentDescriptionActivity extends BaseActivity<ActivityCourseAssignmentDescriptionBinding, CourseAssignmentDescriptionPresenter> implements ICourseAssignmentDescriptionView{

    public static void start(Activity activity, int courseAssignmentId, int courseId) {
        Intent intent = new Intent(activity, CourseAssignmentDescriptionActivity.class);
        intent.putExtra("CourseAssignmentId", courseAssignmentId);
        intent.putExtra("CourseId", courseId);
        activity.startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
    }

    private int courseAssignmentId;
    private int courseId;

    private CourseAssignmentBean courseAssignmentBean;


    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            courseAssignmentId = intent.getIntExtra("CourseAssignmentId", 0);
            courseId = intent.getIntExtra("CourseId", 0);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_course_assignment_description;
    }

    @Override
    protected void initViews() {
        mDataBinding.btnStart.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions("作业");
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        mPresenter.getCourseAssignmentDetailData(courseAssignmentId, courseId);
    }

    @Override
    protected CourseAssignmentDescriptionPresenter getPresenter() {
        return new CourseAssignmentDescriptionPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnStart) {
//            if (courseAssignmentBean.AssignmentType == CourseAssignmentBean.ASSIGNMENT_VIDEO) {
//                CourseAssignmentVideoDetailActivity.start(this, courseAssignmentBean);
//            } else {
//                CourseAssignmentDetailActivity.start(this, courseAssignmentBean);
//            }
            CourseAssignmentDetailActivity.start(this, courseAssignmentBean);
        }
    }

    @Override
    public void configCourseAssignmentDetailData(CourseAssignmentBean data) {
        if (data == null) return;
        this.courseAssignmentBean = data;
        // 修改标题
        mDataBinding.tvAssignmentTitle.setText("作业内容");
        mDataBinding.tvAssignmentContent.setText(courseAssignmentBean.Descp);

        // 设置视频地址
        JZVideoPlayer.releaseAllVideos();
        MediaManager.getInstance().release();
        mDataBinding.videoPlayer.setUp(courseAssignmentBean.VideoUrl, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, courseAssignmentBean.Title);
        // 增加封面
        ImageLoaderProxy.getInstance().displayImage(courseAssignmentBean.ImgUrl, mDataBinding.videoPlayer.thumbImageView);
        JZVideoPlayer.setMediaInterface(new JZMediaIjkplayer());
        mDataBinding.videoPlayer.setJZMediaPlayerListener(new SimpleJZMediaPlayerListener(){
            @Override
            public void onStateAutoComplete() {
                mDataBinding.btnStart.performClick();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_FOR_RESULT) {
            finish();
        }
    }

    @Override
    public void onPause() {
        JZVideoPlayer.goOnPlayOnPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        JZVideoPlayer.releaseAllVideos();
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.abandonAudioFocus(null);
        super.onDestroy();
    }
}
